package com.ssss.build2Args;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

/**
 * 替代多参数构造方法-建造者模式
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ComputerB {

    private String mainBoard;
    private String cpu;
    private String hd;
    private String powerSupplier;// 电源
    private String graphicsCard; // 显卡

    // 其他一些可选配置
    private String mouse;
    private String key;

    // ComputerB 自己充当 Director
    private ComputerB(CompuerBuilder builder){
        this.mainBoard = builder.mainBoard;
        this.cpu = builder.cpu;
        this.hd = builder.hd;
        this.powerSupplier = builder.powerSupplier;
        this.graphicsCard = builder.graphicsCard;

        this.mouse = builder.mouse;
        this.key = builder.key;
    }

    // 声明一个静态的内部类
    public static class CompuerBuilder{
        // 一个电脑的必须配置
        private String mainBoard ;     // 主板
        private String cpu ;           // cpu
        private String hd ;            // 硬盘
        private String powerSupplier ; // 电源
        private String graphicsCard;   // 显卡

        // 其他一些可选配置
        private String mouse;
        private String key;

        // 声明一些必传参数，实际开发中可能都是非必传
//        public CompuerBuilder(String mainBoard, String cpu) {
//            this.mainBoard = mainBoard;
//            this.cpu = cpu;
//        }


        public CompuerBuilder setMainBoard(String mainBoard){
            this.mainBoard = mainBoard;
            return this;
        }

        public CompuerBuilder setCpu(String cpu){
            if (StringUtils.isBlank(cpu)){
                throw new RuntimeException("一定要有cpu");
            }
            this.cpu = cpu;
            return this;
        }

        public CompuerBuilder sethd(String hd){
            this.hd = hd;
            return this;
        }

        public CompuerBuilder setPowerSupplier(String powerSupplier){
            this.powerSupplier = powerSupplier;
            return this;
        }

        public CompuerBuilder setGraphIcsCard(String graphicsCard){
            this.graphicsCard = graphicsCard;
            return this;
        }
        // ... 其他的一些setXXX()方法

        public ComputerB build(){
            return new ComputerB(this);
        }
    }

    // 为了不在外面看见new
    public static CompuerBuilder getComputer(){
        CompuerBuilder compuerBuilder = new CompuerBuilder();
        return compuerBuilder;
    }

}
